package com.zzy.product.controller;

import com.zzy.utils.utils.PageUtils;
import com.zzy.utils.utils.R;
import com.zzy.product.entity.BrandEntity;
import com.zzy.product.service.BrandService;
import com.zzy.utils.validator.group.AddGroup;
import com.zzy.utils.validator.group.UpdateGroup;
import com.zzy.utils.validator.group.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 品牌
 *
 * @author kazu
 * @email kazu_zhang@163.com
 * @date 2023-02-01 21:39:21
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
	
    @Autowired
    private BrandService brandService;
	
	/**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);
		
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody @Validated({AddGroup.class}) BrandEntity brand/*, BindingResult result*/){
//		if (result.hasErrors()) {
//			// 获取校验的错误结果
//			HashMap<String, Object> map = new HashMap<>();
//			result.getFieldErrors().forEach((it) -> {
//				/*获取到错误提示*/
//				String message = it.getDefaultMessage();
//				/*获取错误的属性名字*/
//				String field = it.getField();
//				map.put(field, message);
//			});
//			return R.error(400, "提交数据不合法").put("error", map);
//		} else {
//			brandService.save(brand);
//		}
		brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody @Validated({UpdateGroup.class}) BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }
	
	/**
	 * 只修改status
	 * @param brand
	 * @return
	 */
    @PostMapping("/update/status")
    public R updateStatus(@RequestBody @Validated({UpdateStatusGroup.class}) BrandEntity brand){
		brandService.updateById(brand);
		
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
